package org.tom.petcart.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tom.order.dto.SaveOrderDto;
import org.tom.order.service.IOrderPetService;
import org.tom.petcart.domain.ShopCart;
import org.tom.petcart.dto.SaveCartOrderDto;
import org.tom.petcart.mapper.ShopCartMapper;
import org.tom.petcart.service.IPetCartService;
import org.tom.basic.exception.MyException;
import org.tom.basic.service.impl.BaseServiceImpl;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.LoginContextUtil;
import org.tom.pet.domain.Pet;
import org.tom.pet.mapper.PetMapper;
import org.tom.user.domain.Logininfo;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
@Transactional
public class PetCartServiceImpl extends BaseServiceImpl<ShopCart> implements IPetCartService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrderPetService orderPetService;

    @Override
    public JsonResult addCart(Long petid) {
        Pet pet = petMapper.loadById(petid);
        if (pet.getState() != 1){
            throw new MyException("该宠物的id为下架的宠物");
        }
        ShopCart shopCart = new ShopCart();
        shopCart.setCount(1);
        shopCart.setLogininfo_id(LoginContextUtil.getLoginContext().getId());
        shopCart.setMoney(pet.getSaleprice());
        shopCart.setPet_id(petid);
        shopCart.setState(ShopCart.GO_SHOPCART);
        shopCart.setResource(pet.getResources());
        shopCart.setShop_id(pet.getShop_id());
        shopCartMapper.add(shopCart);

        Logininfo logininfo = LoginContextUtil.getLoginContext();
        String token = ShopCart.SHOPCART_LIST+logininfo.getId();
        List<ShopCart>list = shopCartMapper.loadAllById(logininfo.getId());
        redisTemplate.opsForValue().set(token, list, 30, TimeUnit.MINUTES);


        return JsonResult.me();
    }

    @Override
    public JsonResult getShopCartList() {
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        String token = ShopCart.SHOPCART_LIST+logininfo.getId();
        List<ShopCart> list = (List<ShopCart>) redisTemplate.opsForValue().get(token);
        if (list == null ){
            list = shopCartMapper.loadAllById(logininfo.getId());
            redisTemplate.opsForValue().set(token, list, 30, TimeUnit.MINUTES);
        }
        return JsonResult.me().setResultObj(list);
    }

    @Override
    public JsonResult cal(List<Long> ids) {
        List<ShopCart> list = shopCartMapper.batchShopCatByids(ids);
        BigDecimal moneycount = new BigDecimal(0);
        for (int i=0;i<list.size();i++){
            BigDecimal money = list.get(i).getMoney();
            moneycount = moneycount.add(list.get(i).getMoney());
            System.out.println(moneycount);
        }


        System.out.println(moneycount);
        return JsonResult.me().setResultObj(moneycount);
    }

    @Override
    public String commitPetOrder(SaveCartOrderDto saveCartOrderDtoOrderDto) {
        Long addressId = saveCartOrderDtoOrderDto.getAddressId();
        List<Long> cartId = saveCartOrderDtoOrderDto.getCartId();
        Integer payType = saveCartOrderDtoOrderDto.getPayType();
        String sn = null;
        for (int i=0; i<cartId.size(); i++){
            SaveOrderDto saveOrderDto = new SaveOrderDto();
            saveOrderDto.setAddressId(addressId);
            saveOrderDto.setPayType(payType);
            ShopCart shopCart = shopCartMapper.loadOne(cartId.get(i));
            saveOrderDto.setPetId(shopCart.getPet_id());
            if (i==0){
                sn = orderPetService.commitPetOrder(saveOrderDto);
            }else {
                sn = sn + "," + orderPetService.commitPetOrder(saveOrderDto);
            }
        }

        return sn;
    }

    @Override
    public void delete(Long id) {
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        ShopCart shopCart = shopCartMapper.loadOne(id);
        if (!logininfo.getId().toString().equals(shopCart.getLogininfo_id().toString())){
            throw new MyException("该商品不是你的");
        }
        shopCartMapper.delete(id,logininfo.getId());
        String token = ShopCart.SHOPCART_LIST+logininfo.getId();
        List<ShopCart>list = shopCartMapper.loadAllById(logininfo.getId());
        redisTemplate.opsForValue().set(token, list, 30, TimeUnit.MINUTES);
    }
}
